package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSON;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.pojo.CallStatementListPojo;
import com.xbongbong.paas.pojo.DepUserListPojo;
import com.xbongbong.paas.pojo.dto.CallAccountRegisterDTO;
import com.xbongbong.paas.pojo.dto.CallAccountUserIdInDTO;
import com.xbongbong.paas.pojo.dto.CallCenterAccountUserInfoDTO;
import com.xbongbong.paas.pojo.dto.CallCenterAdminLoginDTO;
import com.xbongbong.paas.pojo.dto.CallEnableAudioTranslateDTO;
import com.xbongbong.paas.pojo.dto.CallGetDepUserListDTO;
import com.xbongbong.paas.pojo.dto.CallOrgRegisterDTO;
import com.xbongbong.paas.pojo.dto.CallOrganizationStatusDTO;
import com.xbongbong.paas.pojo.dto.CallStatementListDTO;
import com.xbongbong.paas.pojo.dto.FeeRemainDTO;
import com.xbongbong.paas.pojo.dto.UserListDTO;
import com.xbongbong.paas.pojo.vo.CallAccountRegisterVO;
import com.xbongbong.paas.pojo.vo.CallCenterAccountUserInfoVO;
import com.xbongbong.paas.pojo.vo.CallCenterAdminLoginVO;
import com.xbongbong.paas.pojo.vo.CallCenterOrgGetByCorpIdVO;
import com.xbongbong.paas.pojo.vo.CallEnableAudioTranslateVO;
import com.xbongbong.paas.pojo.vo.CallGetDepUserListVO;
import com.xbongbong.paas.pojo.vo.CallOrgRegisterVO;
import com.xbongbong.paas.pojo.vo.CallOrganizationStatusVO;
import com.xbongbong.paas.pojo.vo.CallStatementListVO;
import com.xbongbong.paas.pojo.vo.FeeRemainVO;
import com.xbongbong.paas.pojo.vo.UserListVO;
import com.xbongbong.paas.service.CallCenterAccountService;
import com.xbongbong.paas.service.CallCenterAdminService;
import com.xbongbong.paas.service.CallCenterAgentStrategyService;
import com.xbongbong.paas.service.CallCenterCommonService;
import com.xbongbong.paas.service.CallCenterOrganizationService;
import com.xbongbong.paas.service.CallCenterStatementService;
import com.xbongbong.paas.service.UserService;
import com.xbongbong.paas.service.XbbCallTaskLogService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.PageHelperUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.DepartmentVO;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.CallCenterTypeEnum;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.CallCenterErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.CallCenterOrganizationCost;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.constant.callcenter.DuYanConstant;
import com.xbongbong.saas.domain.entity.CallCenterAccountEntity;
import com.xbongbong.saas.domain.entity.CallCenterAgentStrategyEntity;
import com.xbongbong.saas.domain.entity.CallCenterOrganizationEntity;
import com.xbongbong.saas.domain.entity.CallCenterStatementEntity;
import com.xbongbong.saas.model.CallCenterStatementModel;
import com.xbongbong.sys.domain.entity.CompanyEntity;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.model.CompanyModel;
import com.xbongbong.sys.model.DepartmentModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author youli.chen
 * @version v1.0
 * @description: 话务中心 管理 业务层接口实现类
 * @date 2019/2/16 21:13
 * @since v1.0
 */
@Service("callCenterAdminService")
public class CallCenterAdminServiceImpl implements CallCenterAdminService {

    private static final Logger LOG = LoggerFactory.getLogger(CallCenterAdminServiceImpl.class);

    @Resource
    private CallCenterCommonService callCenterCommonService;
    @Resource
    private CallCenterAccountService callCenterAccountService;
    @Resource
    private CallCenterOrganizationService callCenterOrganizationService;
    @Resource
    private CallCenterAgentStrategyService callCenterAgentStrategyService;
    @Resource
    private CallCenterStatementService callCenterStatementService;
    @Resource
    private XbbCallTaskLogService xbbCallTaskLogService;
    @Resource
    private DepartmentModel departmentModel;
    @Resource
    private UserService userService;
    @Resource
    private CompanyModel companyModel;
    @Resource
    private CallCenterStatementModel callCenterStatementModel;

    /**
     * 管理员CFG一键登录获取token
     *
     * @param callCenterAdminLoginDTO 管理员CFG一键登录获取token入参
     * @return CallCenterAdminLoginVO
     * @throws XbbException 业务异常
     */
    @Override
    public XbbResponse<CallCenterAdminLoginVO> login(CallCenterAdminLoginDTO callCenterAdminLoginDTO) throws XbbException {
        CallCenterAdminLoginVO callCenterAdminLoginVO = new CallCenterAdminLoginVO();
        Integer moduleType = callCenterAdminLoginDTO.getModuleType();
        if (callCenterCommonService.notCallCenterModule(moduleType)) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301029);
        }
        CallCenterAccountUserInfoDTO userInfoDTO = new CallCenterAccountUserInfoDTO();
        BeanUtils.copyProperties(callCenterAdminLoginDTO, userInfoDTO);
        CallCenterAccountUserInfoVO userInfoVO = callCenterAccountService.getByXbbUserInfo(userInfoDTO);
        CallCenterAccountEntity callCenterAccountEntity = new CallCenterAccountEntity();
        if (userInfoVO == null || userInfoVO.getIsAdmin() == 0) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301009);
        } else {
            String token;
            try {
                BeanUtils.copyProperties(userInfoVO, callCenterAccountEntity);
                token = callCenterCommonService.getAdminToken(callCenterAdminLoginDTO.getCorpid(),
                        moduleType, callCenterAccountEntity.getAccountId());
            } catch (XbbException e) {
                LOG.error(e.getMessage());
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018);
            }
            if (StringUtil.isEmpty(token)) {
                throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301004);
            }
            callCenterAdminLoginVO.setAccountId(callCenterAccountEntity.getAccountId());
            callCenterAdminLoginVO.setToken(token);
        }
        return new XbbResponse<>(callCenterAdminLoginVO);
    }

    /**
     * 企业注册
     *
     * @param callOrgRegisterDTO 企业注册入参
     * @return CallOrgRegisterVO
     * @throws XbbException 业务异常
     */
    @Override
    public XbbResponse<CallOrgRegisterVO> organizationRegister(CallOrgRegisterDTO callOrgRegisterDTO) throws XbbException {
        CallOrgRegisterVO callOrgRegisterVO = new CallOrgRegisterVO();
        if (!DuYanConstant.API_KEY_PARTNER.equals(callOrgRegisterDTO.getApikey())) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301042);
        }
        CallCenterOrgGetByCorpIdVO getByCorpIdVO = callCenterOrganizationService.getByCorpId(callOrgRegisterDTO.getCorpid(), callOrgRegisterDTO.getModuleType());
        if (getByCorpIdVO == null) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301001);
        }
        try {
            if (!callCenterCommonService.registerOrganization(callOrgRegisterDTO.getCorpid(),
                    callOrgRegisterDTO.getModuleType(),
                    callOrgRegisterDTO.getName(),
                    callOrgRegisterDTO.getAlias(),
                    callOrgRegisterDTO.getRegisterId(),
                    callOrgRegisterDTO.getIsAudioTranslate(),
                    callOrgRegisterDTO.getPlatformLine().getCode(),
                    callOrgRegisterDTO.getUnitPrice())) {
                throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301043);
            }
        } catch (XbbException e) {
            LOG.error("话务中心注册企业时出错：", e);
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301044);
        }
        return new XbbResponse<>(callOrgRegisterVO);
    }

    /**
     * 员工账号注册
     *
     * @param callAccountRegisterDTO 员工账号注册入参
     * @return CallAccountRegisterVO
     * @throws XbbException 业务异常
     */
    @Override
    public XbbResponse<CallAccountRegisterVO> accountRegister(CallAccountRegisterDTO callAccountRegisterDTO) throws XbbException {
        CallAccountRegisterVO callAccountRegisterVO = new CallAccountRegisterVO();
        String corpId = callAccountRegisterDTO.getCorpid();
        Integer moduleType = callAccountRegisterDTO.getModuleType();
        if (callCenterCommonService.notCallCenterModule(moduleType)) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301029);
        }
        String apiKey = callCenterOrganizationService.getApiKey(corpId, moduleType);
        if (StringUtil.isEmpty(apiKey)) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301001);
        }
        callCenterCommonService.registerAccount(apiKey, moduleType, corpId, callAccountRegisterDTO.getRegisterUserId());
        return new XbbResponse<>(callAccountRegisterVO);
    }

    /**
     * 获取企业的坐席数量、通话时间等消费信息
     *
     * @param callOrganizationStatusDTO 获取企业的坐席数量、通话时间等消费信息入参
     * @return CallOrganizationStatusVO  获取企业的坐席数量、通话时间等消费信息回参
     * @throws XbbException 业务异常
     */
    @Override
    public XbbResponse<CallOrganizationStatusVO> organizationStatus(CallOrganizationStatusDTO callOrganizationStatusDTO) throws XbbException {
        CallOrganizationStatusVO statusVO = new CallOrganizationStatusVO();
        String corpid = callOrganizationStatusDTO.getCorpid();
        Integer moduleType = callOrganizationStatusDTO.getModuleType();
        if (callCenterCommonService.notCallCenterModule(moduleType)) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301029);
        }
        Integer agentNum = callCenterAccountService.countAgentNum(corpid, moduleType);
        CallCenterOrgGetByCorpIdVO getByCorpIdVO = callCenterOrganizationService.getByCorpId(corpid, moduleType);
        if (getByCorpIdVO == null) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301001);
        }

        FeeRemainDTO feeRemainDTO = new FeeRemainDTO();
        feeRemainDTO.setCorpid(corpid);
        FeeRemainVO feeRemainVO = xbbCallTaskLogService.getRemain(feeRemainDTO);
        if (feeRemainVO == null) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301045);
        }
        if (Objects.equals(moduleType, CallCenterTypeEnum.CALL_CENTER_DU_YAN.getCode())) {
            // 度言是按月扣费，去除有效时间，失效时间，休眠时间数据
            statusVO.setBalance(getByCorpIdVO.getBalance().setScale(2, BigDecimal.ROUND_DOWN).toString());
            statusVO.setAgentNum(agentNum);
            CallCenterAgentStrategyEntity strategy = callCenterAgentStrategyService.getActiveStrategy(corpid, moduleType);
            statusVO.setAgentPrice(strategy.getPrice().setScale(2, BigDecimal.ROUND_UP).toString());
            statusVO.setPaidAgentNum(getByCorpIdVO.getPaidAgentNum());
            BigDecimal curMonthCost = callCenterStatementService.checkCurrentMonthCost(corpid, moduleType);
            statusVO.setCurMonthCost(curMonthCost.setScale(2, BigDecimal.ROUND_UP).toString());
            Boolean isEnough4NextMonth = getByCorpIdVO.getBalance().compareTo(strategy.getPrice().multiply(new BigDecimal(agentNum))) < 0;
            statusVO.setIsEnough4NextMonth(isEnough4NextMonth);
        } else if (Objects.equals(moduleType, CallCenterTypeEnum.CALL_CENTER_XBB.getCode())) {
            statusVO.setXbbAgentNum(getByCorpIdVO.getXbbAgentNum());
            statusVO.setAgentNum(agentNum);
            Integer startTime = getByCorpIdVO.getStartTime() == null ? Integer.valueOf(0) : getByCorpIdVO.getStartTime();
            statusVO.setStartTime(DateUtil.getDateString(startTime));
            Integer endTime = getByCorpIdVO.getEndTime() == null ? Integer.valueOf(0) : getByCorpIdVO.getEndTime();
            statusVO.setEndTime(DateUtil.getDateString(endTime));
            statusVO.setGetRemain(feeRemainVO.getGetRemain());
        }
        return new XbbResponse<>(statusVO);
    }

    /**
     * 获取部门的员工列表
     *
     * @param callGetDepUserListDTO 获取部门的员工列表入参
     * @return CallGetDepUserListVO  获取部门的员工列表回参
     * @throws XbbException 业务异常
     */
    @Override
    public XbbResponse<CallGetDepUserListVO> getDepUserList(CallGetDepUserListDTO callGetDepUserListDTO) throws XbbException {
        CallGetDepUserListVO depUserListVO = new CallGetDepUserListVO();
        String corpid = callGetDepUserListDTO.getCorpid();
        Integer moduleType = callGetDepUserListDTO.getModuleType();
        Long depId = callGetDepUserListDTO.getDepId();
        if (callCenterCommonService.notCallCenterModule(moduleType)) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301029);
        }
        // 显示隐藏部门
        List<DepartmentEntity> allDepartmentList = departmentModel.findAllDepartment(corpid, true);
        Map<Long, DepartmentEntity> allDepartmentMap = new HashMap<>(16);
        for (DepartmentEntity it : allDepartmentList) {
            allDepartmentMap.put(it.getId(), it);
        }
        UserListDTO userListDTO = new UserListDTO();
        BeanUtils.copyProperties(callGetDepUserListDTO, userListDTO);
        userListDTO.setDepartmentId(callGetDepUserListDTO.getDepId());
        UserListVO userListVO = userService.list(userListDTO);
        List<UserVO> userList = userListVO.getUserList();
        List<CallCenterAccountEntity> callCenterAccountEntityList;
        if (Objects.equals(userList.size(), 0)) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301046);
        }
        List<String> userIdIn = new ArrayList<>();
        for (UserVO user : userList) {
            userIdIn.add(user.getUserId());
        }
        //兼容部门ID不是1的坐席 youli.chen
        CallAccountUserIdInDTO callAccountUserIdInDTO = new CallAccountUserIdInDTO();
        callAccountUserIdInDTO.setCorpid(corpid);
        callAccountUserIdInDTO.setModuleType(moduleType);
        callAccountUserIdInDTO.setUserIdIn(userIdIn);
        callCenterAccountEntityList = callCenterAccountService.getByUserIdIn(callAccountUserIdInDTO);
//        if (Objects.equals(depId, PaasConstant.CORP_DEPARTMENT_ID)) {
//            callCenterAccountEntityList = callCenterAccountService.getByUserIdIn(callAccountUserIdInDTO);
//        } else {
//            callAccountUserIdInDTO.setDepId(depId);
//            callCenterAccountEntityList = callCenterAccountService.getByUserIdIn(callAccountUserIdInDTO);
//        }
        Map<String, CallCenterAccountEntity> callCenterAccountEntityMap = new HashMap<>(callCenterAccountEntityList.size());
        if (callCenterAccountEntityList.size() > 0) {
            for (CallCenterAccountEntity it : callCenterAccountEntityList) {
                callCenterAccountEntityMap.put(it.getUserId(), it);
            }
        }
        List<DepUserListPojo> depUserList = new ArrayList<>();
        for (UserVO user : userList) {
            Set<String> departmentList = new HashSet<>();
            for (DepartmentVO it : user.getDepSet()) {
                departmentList.add(it.getName());
            }
            DepUserListPojo depUserListPojo = new DepUserListPojo();
            depUserListPojo.setUserId(user.getUserId());
            // 传一个和userId一样的id，前端方面展示多选
            depUserListPojo.setId(user.getUserId());
            // 显示值设为value
            depUserListPojo.setValue(user.getName());
            depUserListPojo.setAvatar(user.getAvatar());
            depUserListPojo.setDepartment(departmentList);
            // 话务中心角色列表
            CallCenterAccountEntity callCenterAccountEntity = callCenterAccountEntityMap.get(user.getUserId());
            if (callCenterAccountEntity != null) {
                DepartmentEntity dep = allDepartmentMap.get(callCenterAccountEntity.getDepId());
                depUserListPojo.setCallCenterTeam(dep == null ? "" : dep.getName());
                depUserListPojo.setAgent(Objects.equals(callCenterAccountEntity.getIsAgent(), 1));
                depUserListPojo.setSupervisor(Objects.equals(callCenterAccountEntity.getIsSupervisor(), 1));
                depUserListPojo.setAdmin(Objects.equals(callCenterAccountEntity.getIsAdmin(), 1));
            }
            depUserList.add(depUserListPojo);
        }
        depUserListVO.setDepUserList(depUserList);
        depUserListVO.setPageHelper(userListVO.getPageHelper());
        return new XbbResponse<>(depUserListVO);
    }

    /**
     * 查找坐席费用明细
     *
     * @param callStatementListDTO 查找坐席费用明细入参
     * @return CallStatementListVO  查找坐席费用明细回参
     * @throws XbbException 业务异常
     */
    @Override
    public XbbResponse<CallStatementListVO> statementList(CallStatementListDTO callStatementListDTO) throws XbbException {
        CallStatementListVO callStatementListVO = new CallStatementListVO();
        Integer moduleType = callStatementListDTO.getModuleType();
        String corpid = callStatementListDTO.getCorpid();
        callStatementListDTO.initQueryTimeRange();
        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", corpid);
        params.put("negDel", DelEnum.DELETE.getDel());
        params.put("moduleType", moduleType);
        params.put("type", callStatementListDTO.getType());
        if (StringUtil.isNotEmpty(callStatementListDTO.getCorpName())) {
            List<String> corpIdIn = companyModel.getCorpIdByCorpNameLike(callStatementListDTO.getCorpName());
            if (corpIdIn.size() == 0) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100011);
            }
            params.put("corpIdIn", corpIdIn);
        }
        params.put("startAddTime", callStatementListDTO.getStartTime());
        params.put("endAddTime", callStatementListDTO.getEndTime());
        params.put("page", callStatementListDTO.getPage());
        params.put("orderByStr", "add_time DESC");
        params.put("columns", "corpid,module_type,amount,agent_name_array,type,memo,add_time");
        PageHelper pageHelper = new PageHelper(callStatementListDTO.getPage(), callStatementListDTO.getPageSize());
        List<CallCenterStatementEntity> callStatementEntityList = (List<CallCenterStatementEntity>)
                PageHelperUtil.getEntityList(params, pageHelper, callCenterStatementModel);

        if (callStatementEntityList.size() == 0) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301047);
        }
        List<String> corpIdList = formatAgentNameArray(callStatementEntityList);
        List<CompanyEntity> corpList = companyModel.getCompanyList(corpIdList);
        if (corpList.size() == 0) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301048);
        }
        Map<String, String> corpNameMap = new HashMap<>(16);
        for (CompanyEntity it : corpList) {
            corpNameMap.put(it.getCorpid(), it.getCorpName());
        }
        callStatementEntityList(corpid, moduleType, callStatementEntityList, callStatementListDTO, corpNameMap);
        List<CallStatementListPojo> statementList = new ArrayList<>();
        try {
            BeanUtil.copyPropertiesList(callStatementEntityList, statementList, CallStatementListPojo.class);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        callStatementListVO.setPageHelper(pageHelper);
        callStatementListVO.setStatementList(statementList);
        return new XbbResponse<>(callStatementListVO);
    }

    /**
     * 是否允许公司使用话务中心的翻译功能
     *
     * @param callEnableAudioTranslateDTO 是否允许公司使用话务中心的翻译功能入参
     * @return CallEnableAudioTranslateVO  是否允许公司使用话务中心的翻译功能回参
     * @throws XbbException 业务异常
     */
    @Override
    public XbbResponse<CallEnableAudioTranslateVO> enableAudioTranslate(CallEnableAudioTranslateDTO callEnableAudioTranslateDTO) throws XbbException {
        CallEnableAudioTranslateVO translateVO = new CallEnableAudioTranslateVO();
        Integer moduleType = callEnableAudioTranslateDTO.getModuleType();
        Boolean audioTranslate = callEnableAudioTranslateDTO.getAudioTranslate();
        if (callCenterCommonService.notCallCenterModule(moduleType)) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301029);
        }
        CallCenterOrgGetByCorpIdVO getByCorpIdVO = callCenterOrganizationService.getByCorpId(callEnableAudioTranslateDTO.getCorpid(), moduleType);
        if (getByCorpIdVO == null) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301001);
        }
        if (Objects.equals(getByCorpIdVO.getIsAudioTranslate(), audioTranslate)) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301049);
        }
        CallCenterOrganizationEntity org = new CallCenterOrganizationEntity();
        BeanUtils.copyProperties(getByCorpIdVO,org);
        callCenterOrganizationService.enableAudioTranslate(org, audioTranslate);
        return new XbbResponse<>(translateVO);
    }

    /**
     * 将坐席名称字符串转为队列并得到公司id队列
     *
     * @param list 对账单
     * @return 公司id队列
     * @author 章华隽
     * 创建时间：2018/4/3 下午3:19
     * 修改时间：
     * @version 3.20
     * @since 3.20
     */
    private List<String> formatAgentNameArray(List<CallCenterStatementEntity> list) {
        Set<String> corpIdSet = new HashSet<>();
        for (CallCenterStatementEntity it : list) {
            it.setAmount(it.getAmount().setScale(2, BigDecimal.ROUND_FLOOR));
            corpIdSet.add(it.getCorpid());
            try {
                it.setAgentNameList(JSON.parseArray(it.getAgentNameArray(), String.class));
                it.setAgentNum(it.getAgentNameList().size());
            } catch (Exception e) {
                it.setAgentNameList(new ArrayList<>());
                it.setAgentNum(0);
            } finally {
                it.setAgentNameArray(null);
            }
        }
        return new ArrayList<>(corpIdSet);
    }

    /**
     * 度言数据封装
     *
     * @param corpid                  公司ID
     * @param moduleType              微应用标识，参考CallCenterTypeEnum
     * @param callStatementEntityList 话务中心坐席策略对账单列表
     * @param callStatementListDTO    查找坐席费用明细入参
     * @param corpNameMap             公司名称map
     * @return
     * @throws XbbException
     */
    private void callStatementEntityList(String corpid, Integer moduleType,
                                                                    List<CallCenterStatementEntity> callStatementEntityList,
                                                                    CallStatementListDTO callStatementListDTO,
                                                                    Map<String, String> corpNameMap) throws XbbException {
        // 排除掉销帮帮呼叫中心的数据 youli.chen
        if (!Objects.equals(moduleType, CallCenterTypeEnum.CALL_CENTER_XBB.getCode())) {
            CallCenterOrgGetByCorpIdVO getByCorpIdVO = callCenterOrganizationService.getByCorpId(corpid, moduleType);
            if (getByCorpIdVO == null) {
                throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301001);
            }
            CallCenterAgentStrategyEntity strategy = callCenterAgentStrategyService.getActiveStrategy(corpid, moduleType);
            if (callStatementListDTO.getCheckBill() != null) {
                if (Objects.equals(callStatementListDTO.getCheckBill(), StringConstant.PLATFORM_DUYAN)) {
                    for (CallCenterStatementEntity it : callStatementEntityList) {
                        it.setCorpName(corpNameMap.get(it.getCorpid()));
                        if (getByCorpIdVO.getPlatformLine() == null || getByCorpIdVO.getPlatformLine() == 0) {
                            it.setPlatformLine(I18nMessageUtil.getMessage(I18nStringConstant.NOT_ACCESS_PLAN));
                        } else {
                            it.setPlatformLine(CallCenterOrganizationCost.PlatformLine.getByCode(getByCorpIdVO.getPlatformLine()).getName());
                        }
                        it.setUnitPrice(getByCorpIdVO.getUnitPrice());

                        BigDecimal agentPrice = new BigDecimal(it.getUnitPrice().split(I18nMessageUtil.getMessage(I18nStringConstant.ELEMENT))[0]);
                        BigDecimal multiplyPrice = (it.getAmount().abs()).multiply(agentPrice);
                        if (strategy.getPrice().equals(BigDecimal.ZERO) || strategy.getPrice() == null ||
                                agentPrice.equals(BigDecimal.ZERO) || multiplyPrice.equals(BigDecimal.ZERO)) {
                            it.setAmountShow("0.00");
                        } else {
                            it.setAmountShow((multiplyPrice.divide(strategy.getPrice(), 2, RoundingMode.HALF_UP)).toString());
                        }

                    }
                }
            } else {
                for (CallCenterStatementEntity it : callStatementEntityList) {
                    it.setCorpName(corpNameMap.get(it.getCorpid()));
                    it.setAmountShow(it.getAmount().setScale(2, BigDecimal.ROUND_DOWN).toString());
                    if (getByCorpIdVO.getPlatformLine() == null || getByCorpIdVO.getPlatformLine() == 0) {
                        it.setPlatformLine(I18nMessageUtil.getMessage(I18nStringConstant.NOT_ACCESS_PLAN));
                    } else {
                        it.setPlatformLine(CallCenterOrganizationCost.PlatformLine.getByCode(getByCorpIdVO.getPlatformLine()).getName());
                    }
                    it.setUnitPrice(getByCorpIdVO.getUnitPrice());
                }
            }
        }
    }
}
